package com.zyg.tools;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

//数据库连接   工具类
public class DBUtil {

	static String username = "root";
	static String password = "asd123";
	static String url = "jdbc:mysql://localhost:3306/spring?useUnicode=true&characterEncoding=utf-8";
	static Connection connection ;
	static{
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
	//定义一个getConnection用户获取数据库链接
	public static Connection getConnection() throws SQLException{
		connection = DriverManager.getConnection(url,username,password);
		return connection;
	}
	//关闭数据库操作中的对象
	public static void  closeAll(Connection ct,PreparedStatement prepareStatement,ResultSet rs){
		
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (prepareStatement != null) {
			try {
				prepareStatement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if (ct != null) {
			try {
				ct.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
